{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4a1a5970",
   "metadata": {
    "papermill": {
     "duration": 0.067527,
     "end_time": "2022-03-30T00:41:38.166135",
     "exception": false,
     "start_time": "2022-03-30T00:41:38.098608",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 0.Overview\n",
    "**Tutorial**: lstm-model-with-item-infor-fix-missing-last-item\n",
    "\n",
    "**Author**: [astrung](https://github.com/astrung)\n",
    "\n",
    "**Original link**: [notebook](https://www.kaggle.com/code/astrung/lstm-model-with-item-infor-fix-missing-last-item)\n",
    "\n",
    "**Edit:**\n",
    "\n",
    "* In my previous notebooks([here](https://www.kaggle.com/code/astrung/lstm-sequential-modelwith-item-features-tutorial) and [here](https://www.kaggle.com/code/astrung/lstm-sequential-modelwith-item-features-tutorial)), we have used test_data with `full_sort_topk`,but due to the limit of full_sort_topk we have missed last item for submited recommendation. Someone asked me about how can use all items as input features for recommendation in this [comment](https://www.kaggle.com/code/astrung/recbole-lstm-sequential-for-recomendation-tutorial/comments#1723707). \n",
    "* So i created a notebook [here](https://www.kaggle.com/code/astrung/recbole-using-all-items-for-prediction) for address there questions in detail, and this notebook is an improved of my [previous notebook](https://www.kaggle.com/code/astrung/lstm-sequential-modelwith-item-features-tutorial), applying our new function (using all item as input features without `full_sort_topk`) for this competition. In this notebook, we also use item features as input.\n",
    "* If you only want to use interaction as input feature, please check this [notebook](https://www.kaggle.com/astrung/lstm-model-with-item-infor-fix-missing-last-item).\n",
    "\n",
    "- - -\n",
    "\n",
    "In previous [my notebook](https://www.kaggle.com/code/astrung/sequential-model-fixed-missing-last-item/), we tried to use GRU/LSTM model for testing effect of sequential model for recommendation with only iteration.\n",
    "In this notebook, i showed how we can enhance sequential model with item features \n",
    "\n",
    "Due to memory limit and faster testing purpose, we will just use data in 2020.\n",
    "\n",
    "If you want to use with all of interactions in all time, i have created a new atomic dataset here for you: \n",
    "\n",
    "* only interations data: https://www.kaggle.com/astrung/hm-atomic-interation\n",
    "* iterations + item features data: https://www.kaggle.com/astrung/hm-atomic-interation-with-item-feature \n",
    "\n",
    "We also have other limit: we only train model and predict with users who buy more than 40 items and items which is bought by more than 40 people.\n",
    "\n",
    "We will follow below steps for creating model:\n",
    "\n",
    "1. In order to use Recbole, we create atomic file from interaction data and item data\n",
    "2. Because we only use Recbole model for predicting with users who buy more than 40 items, other users will need to fill by default recomendation items. We create most viewed items in last month as defautl recomendation\n",
    "3. We create dataset and train model in recbole.\n",
    "4. We create prediction result by trained model\n",
    "5. We combine recomendation result from most viewed items in last month and Recbole predicted model.\n",
    "\n",
    "I will explain more detail in following cells.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f2a64764",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:41:38.271362Z",
     "iopub.status.busy": "2022-03-30T00:41:38.270252Z",
     "iopub.status.idle": "2022-03-30T00:41:59.220822Z",
     "shell.execute_reply": "2022-03-30T00:41:59.220114Z",
     "shell.execute_reply.started": "2022-03-20T03:28:15.953594Z"
    },
    "papermill": {
     "duration": 21.009084,
     "end_time": "2022-03-30T00:41:59.221012",
     "exception": false,
     "start_time": "2022-03-30T00:41:38.211928",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting recbole\r\n",
      "  Downloading recbole-1.0.1-py3-none-any.whl (2.0 MB)\r\n",
      "     |████████████████████████████████| 2.0 MB 533 kB/s            \r\n",
      "\u001b[?25hCollecting scipy==1.6.0\r\n",
      "  Downloading scipy-1.6.0-cp37-cp37m-manylinux1_x86_64.whl (27.4 MB)\r\n",
      "     |████████████████████████████████| 27.4 MB 100 kB/s             \r\n",
      "\u001b[?25hRequirement already satisfied: pandas>=1.0.5 in /opt/conda/lib/python3.7/site-packages (from recbole) (1.3.5)\r\n",
      "Collecting colorlog==4.7.2\r\n",
      "  Downloading colorlog-4.7.2-py2.py3-none-any.whl (10 kB)\r\n",
      "Requirement already satisfied: colorama==0.4.4 in /opt/conda/lib/python3.7/site-packages (from recbole) (0.4.4)\r\n",
      "Requirement already satisfied: tqdm>=4.48.2 in /opt/conda/lib/python3.7/site-packages (from recbole) (4.62.3)\r\n",
      "Requirement already satisfied: pyyaml>=5.1.0 in /opt/conda/lib/python3.7/site-packages (from recbole) (6.0)\r\n",
      "Requirement already satisfied: scikit-learn>=0.23.2 in /opt/conda/lib/python3.7/site-packages (from recbole) (0.23.2)\r\n",
      "Requirement already satisfied: torch>=1.7.0 in /opt/conda/lib/python3.7/site-packages (from recbole) (1.9.1)\r\n",
      "Requirement already satisfied: numpy>=1.17.2 in /opt/conda/lib/python3.7/site-packages (from recbole) (1.20.3)\r\n",
      "Requirement already satisfied: tensorboard>=2.5.0 in /opt/conda/lib/python3.7/site-packages (from recbole) (2.6.0)\r\n",
      "Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/lib/python3.7/site-packages (from pandas>=1.0.5->recbole) (2.8.2)\r\n",
      "Requirement already satisfied: pytz>=2017.3 in /opt/conda/lib/python3.7/site-packages (from pandas>=1.0.5->recbole) (2021.3)\r\n",
      "Requirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.7/site-packages (from scikit-learn>=0.23.2->recbole) (1.1.0)\r\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn>=0.23.2->recbole) (3.0.0)\r\n",
      "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (1.8.0)\r\n",
      "Requirement already satisfied: grpcio>=1.24.3 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (1.43.0)\r\n",
      "Requirement already satisfied: absl-py>=0.4 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (0.15.0)\r\n",
      "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (0.6.1)\r\n",
      "Requirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (1.35.0)\r\n",
      "Requirement already satisfied: markdown>=2.6.8 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (3.3.6)\r\n",
      "Requirement already satisfied: wheel>=0.26 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (0.37.0)\r\n",
      "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (0.4.6)\r\n",
      "Requirement already satisfied: setuptools>=41.0.0 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (59.5.0)\r\n",
      "Requirement already satisfied: werkzeug>=0.11.15 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (2.0.2)\r\n",
      "Requirement already satisfied: protobuf>=3.6.0 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (3.19.1)\r\n",
      "Requirement already satisfied: requests<3,>=2.21.0 in /opt/conda/lib/python3.7/site-packages (from tensorboard>=2.5.0->recbole) (2.26.0)\r\n",
      "Requirement already satisfied: typing-extensions in /opt/conda/lib/python3.7/site-packages (from torch>=1.7.0->recbole) (4.0.1)\r\n",
      "Requirement already satisfied: six in /opt/conda/lib/python3.7/site-packages (from absl-py>=0.4->tensorboard>=2.5.0->recbole) (1.16.0)\r\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=2.5.0->recbole) (0.2.7)\r\n",
      "Requirement already satisfied: cachetools<5.0,>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=2.5.0->recbole) (4.2.4)\r\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /opt/conda/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=2.5.0->recbole) (4.8)\r\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.5.0->recbole) (1.3.0)\r\n",
      "Requirement already satisfied: importlib-metadata>=4.4 in /opt/conda/lib/python3.7/site-packages (from markdown>=2.6.8->tensorboard>=2.5.0->recbole) (4.10.1)\r\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard>=2.5.0->recbole) (3.1)\r\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard>=2.5.0->recbole) (2.0.9)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard>=2.5.0->recbole) (2021.10.8)\r\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard>=2.5.0->recbole) (1.26.7)\r\n",
      "Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.7/site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard>=2.5.0->recbole) (3.6.0)\r\n",
      "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /opt/conda/lib/python3.7/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard>=2.5.0->recbole) (0.4.8)\r\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.5.0->recbole) (3.1.1)\r\n",
      "Installing collected packages: scipy, colorlog, recbole\r\n",
      "  Attempting uninstall: scipy\r\n",
      "    Found existing installation: scipy 1.7.3\r\n",
      "    Uninstalling scipy-1.7.3:\r\n",
      "      Successfully uninstalled scipy-1.7.3\r\n",
      "  Attempting uninstall: colorlog\r\n",
      "    Found existing installation: colorlog 6.6.0\r\n",
      "    Uninstalling colorlog-6.6.0:\r\n",
      "      Successfully uninstalled colorlog-6.6.0\r\n",
      "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\r\n",
      "yellowbrick 1.3.post1 requires numpy<1.20,>=1.16.0, but you have numpy 1.20.3 which is incompatible.\r\n",
      "pdpbox 0.2.1 requires matplotlib==3.1.1, but you have matplotlib 3.5.1 which is incompatible.\r\n",
      "imbalanced-learn 0.9.0 requires scikit-learn>=1.0.1, but you have scikit-learn 0.23.2 which is incompatible.\r\n",
      "featuretools 1.4.1 requires numpy>=1.21.0, but you have numpy 1.20.3 which is incompatible.\r\n",
      "arviz 0.11.4 requires typing-extensions<4,>=3.7.4.3, but you have typing-extensions 4.0.1 which is incompatible.\u001b[0m\r\n",
      "Successfully installed colorlog-4.7.2 recbole-1.0.1 scipy-1.6.0\r\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\r\n"
     ]
    }
   ],
   "source": [
    "!pip install recbole"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d55f3b0c",
   "metadata": {
    "papermill": {
     "duration": 0.069231,
     "end_time": "2022-03-30T00:41:59.365650",
     "exception": false,
     "start_time": "2022-03-30T00:41:59.296419",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 1. Create atomic file"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4ed7947",
   "metadata": {
    "papermill": {
     "duration": 0.070207,
     "end_time": "2022-03-30T00:41:59.507241",
     "exception": false,
     "start_time": "2022-03-30T00:41:59.437034",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 1.A create atomic of item features\n",
    "we will create item features for feeding with iteration features into GRU4REC model "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a2d005de",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:41:59.698974Z",
     "iopub.status.busy": "2022-03-30T00:41:59.695833Z",
     "iopub.status.idle": "2022-03-30T00:42:01.232285Z",
     "shell.execute_reply": "2022-03-30T00:42:01.232821Z",
     "shell.execute_reply.started": "2022-03-20T03:28:32.486357Z"
    },
    "papermill": {
     "duration": 1.655417,
     "end_time": "2022-03-30T00:42:01.232989",
     "exception": false,
     "start_time": "2022-03-30T00:41:59.577572",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>article_id</th>\n",
       "      <th>product_code</th>\n",
       "      <th>prod_name</th>\n",
       "      <th>product_type_no</th>\n",
       "      <th>product_type_name</th>\n",
       "      <th>product_group_name</th>\n",
       "      <th>graphical_appearance_no</th>\n",
       "      <th>graphical_appearance_name</th>\n",
       "      <th>colour_group_code</th>\n",
       "      <th>colour_group_name</th>\n",
       "      <th>...</th>\n",
       "      <th>department_name</th>\n",
       "      <th>index_code</th>\n",
       "      <th>index_name</th>\n",
       "      <th>index_group_no</th>\n",
       "      <th>index_group_name</th>\n",
       "      <th>section_no</th>\n",
       "      <th>section_name</th>\n",
       "      <th>garment_group_no</th>\n",
       "      <th>garment_group_name</th>\n",
       "      <th>detail_desc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0108775015</td>\n",
       "      <td>108775</td>\n",
       "      <td>Strap top</td>\n",
       "      <td>253</td>\n",
       "      <td>Vest top</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>Solid</td>\n",
       "      <td>9</td>\n",
       "      <td>Black</td>\n",
       "      <td>...</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>A</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>1</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>16</td>\n",
       "      <td>Womens Everyday Basics</td>\n",
       "      <td>1002</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>Jersey top with narrow shoulder straps.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0108775044</td>\n",
       "      <td>108775</td>\n",
       "      <td>Strap top</td>\n",
       "      <td>253</td>\n",
       "      <td>Vest top</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>Solid</td>\n",
       "      <td>10</td>\n",
       "      <td>White</td>\n",
       "      <td>...</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>A</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>1</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>16</td>\n",
       "      <td>Womens Everyday Basics</td>\n",
       "      <td>1002</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>Jersey top with narrow shoulder straps.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0108775051</td>\n",
       "      <td>108775</td>\n",
       "      <td>Strap top (1)</td>\n",
       "      <td>253</td>\n",
       "      <td>Vest top</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010017</td>\n",
       "      <td>Stripe</td>\n",
       "      <td>11</td>\n",
       "      <td>Off White</td>\n",
       "      <td>...</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>A</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>1</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>16</td>\n",
       "      <td>Womens Everyday Basics</td>\n",
       "      <td>1002</td>\n",
       "      <td>Jersey Basic</td>\n",
       "      <td>Jersey top with narrow shoulder straps.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0110065001</td>\n",
       "      <td>110065</td>\n",
       "      <td>OP T-shirt (Idro)</td>\n",
       "      <td>306</td>\n",
       "      <td>Bra</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>Solid</td>\n",
       "      <td>9</td>\n",
       "      <td>Black</td>\n",
       "      <td>...</td>\n",
       "      <td>Clean Lingerie</td>\n",
       "      <td>B</td>\n",
       "      <td>Lingeries/Tights</td>\n",
       "      <td>1</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>61</td>\n",
       "      <td>Womens Lingerie</td>\n",
       "      <td>1017</td>\n",
       "      <td>Under-, Nightwear</td>\n",
       "      <td>Microfibre T-shirt bra with underwired, moulde...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0110065002</td>\n",
       "      <td>110065</td>\n",
       "      <td>OP T-shirt (Idro)</td>\n",
       "      <td>306</td>\n",
       "      <td>Bra</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>Solid</td>\n",
       "      <td>10</td>\n",
       "      <td>White</td>\n",
       "      <td>...</td>\n",
       "      <td>Clean Lingerie</td>\n",
       "      <td>B</td>\n",
       "      <td>Lingeries/Tights</td>\n",
       "      <td>1</td>\n",
       "      <td>Ladieswear</td>\n",
       "      <td>61</td>\n",
       "      <td>Womens Lingerie</td>\n",
       "      <td>1017</td>\n",
       "      <td>Under-, Nightwear</td>\n",
       "      <td>Microfibre T-shirt bra with underwired, moulde...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   article_id  product_code          prod_name  product_type_no  \\\n",
       "0  0108775015        108775          Strap top              253   \n",
       "1  0108775044        108775          Strap top              253   \n",
       "2  0108775051        108775      Strap top (1)              253   \n",
       "3  0110065001        110065  OP T-shirt (Idro)              306   \n",
       "4  0110065002        110065  OP T-shirt (Idro)              306   \n",
       "\n",
       "  product_type_name  product_group_name  graphical_appearance_no  \\\n",
       "0          Vest top  Garment Upper body                  1010016   \n",
       "1          Vest top  Garment Upper body                  1010016   \n",
       "2          Vest top  Garment Upper body                  1010017   \n",
       "3               Bra           Underwear                  1010016   \n",
       "4               Bra           Underwear                  1010016   \n",
       "\n",
       "  graphical_appearance_name  colour_group_code colour_group_name  ...  \\\n",
       "0                     Solid                  9             Black  ...   \n",
       "1                     Solid                 10             White  ...   \n",
       "2                    Stripe                 11         Off White  ...   \n",
       "3                     Solid                  9             Black  ...   \n",
       "4                     Solid                 10             White  ...   \n",
       "\n",
       "   department_name index_code        index_name index_group_no  \\\n",
       "0     Jersey Basic          A        Ladieswear              1   \n",
       "1     Jersey Basic          A        Ladieswear              1   \n",
       "2     Jersey Basic          A        Ladieswear              1   \n",
       "3   Clean Lingerie          B  Lingeries/Tights              1   \n",
       "4   Clean Lingerie          B  Lingeries/Tights              1   \n",
       "\n",
       "   index_group_name section_no            section_name garment_group_no  \\\n",
       "0        Ladieswear         16  Womens Everyday Basics             1002   \n",
       "1        Ladieswear         16  Womens Everyday Basics             1002   \n",
       "2        Ladieswear         16  Womens Everyday Basics             1002   \n",
       "3        Ladieswear         61         Womens Lingerie             1017   \n",
       "4        Ladieswear         61         Womens Lingerie             1017   \n",
       "\n",
       "   garment_group_name                                        detail_desc  \n",
       "0        Jersey Basic            Jersey top with narrow shoulder straps.  \n",
       "1        Jersey Basic            Jersey top with narrow shoulder straps.  \n",
       "2        Jersey Basic            Jersey top with narrow shoulder straps.  \n",
       "3   Under-, Nightwear  Microfibre T-shirt bra with underwired, moulde...  \n",
       "4   Under-, Nightwear  Microfibre T-shirt bra with underwired, moulde...  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import gc\n",
    "df = pd.read_csv(r\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\", dtype={'article_id': 'str'})\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2633bb84",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:42:01.392374Z",
     "iopub.status.busy": "2022-03-30T00:42:01.391390Z",
     "iopub.status.idle": "2022-03-30T00:42:01.553293Z",
     "shell.execute_reply": "2022-03-30T00:42:01.552314Z",
     "shell.execute_reply.started": "2022-03-20T03:28:33.491829Z"
    },
    "papermill": {
     "duration": 0.248859,
     "end_time": "2022-03-30T00:42:01.553446",
     "exception": false,
     "start_time": "2022-03-30T00:42:01.304587",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "article_id\n",
      "105542\n",
      "product_code\n",
      "47224\n",
      "prod_name\n",
      "45875\n",
      "product_type_no\n",
      "132\n",
      "product_type_name\n",
      "131\n",
      "product_group_name\n",
      "19\n",
      "graphical_appearance_no\n",
      "30\n",
      "graphical_appearance_name\n",
      "30\n",
      "colour_group_code\n",
      "50\n",
      "colour_group_name\n",
      "50\n",
      "perceived_colour_value_id\n",
      "8\n",
      "perceived_colour_value_name\n",
      "8\n",
      "perceived_colour_master_id\n",
      "20\n",
      "perceived_colour_master_name\n",
      "20\n",
      "department_no\n",
      "299\n",
      "department_name\n",
      "250\n",
      "index_code\n",
      "10\n",
      "index_name\n",
      "10\n",
      "index_group_no\n",
      "5\n",
      "index_group_name\n",
      "5\n",
      "section_no\n",
      "57\n",
      "section_name\n",
      "56\n",
      "garment_group_no\n",
      "21\n",
      "garment_group_name\n",
      "21\n",
      "detail_desc\n",
      "43405\n"
     ]
    }
   ],
   "source": [
    "for col in df.columns:\n",
    "    print(col)\n",
    "    print(len(pd.unique(df[col])))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df90ab4d",
   "metadata": {
    "papermill": {
     "duration": 0.074378,
     "end_time": "2022-03-30T00:42:01.703487",
     "exception": false,
     "start_time": "2022-03-30T00:42:01.629109",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "#### we see so many couple of columns are [category_text, encoded_value]. So in order to avoid [Multicollinearity](https://link.springer.com/chapter/10.1007/978-0-585-25657-3_37), we will keep only one columns in each couple\n",
    "We can see below couple of columns in item features, and we will keep one of them:\n",
    "\n",
    "* use product_type_no - skip product_type_name\n",
    "* use graphical_appearance_no - skip graphical_appearance_name\n",
    "* use colour_group_code - skip colour_group_name\n",
    "* use perceived_colour_value_id - skip perceived_colour_value_name\n",
    "* use perceived_colour_master_id - skip perceived_colour_master_name\n",
    "* use index_code - skip index_name\n",
    "* use index_group_no - skip index_group_name\n",
    "* use section_no - skip section_name\n",
    "* use garment_group_no - skip garment_group_name\n",
    "* use product_code, skip product_name\n",
    "* use department_no, skip department_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d3defd2d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:42:01.856833Z",
     "iopub.status.busy": "2022-03-30T00:42:01.855783Z",
     "iopub.status.idle": "2022-03-30T00:42:01.877762Z",
     "shell.execute_reply": "2022-03-30T00:42:01.878722Z",
     "shell.execute_reply.started": "2022-03-20T03:28:33.646379Z"
    },
    "papermill": {
     "duration": 0.10181,
     "end_time": "2022-03-30T00:42:01.878893",
     "exception": false,
     "start_time": "2022-03-30T00:42:01.777083",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>article_id</th>\n",
       "      <th>product_code</th>\n",
       "      <th>product_type_no</th>\n",
       "      <th>product_group_name</th>\n",
       "      <th>graphical_appearance_no</th>\n",
       "      <th>colour_group_code</th>\n",
       "      <th>perceived_colour_value_id</th>\n",
       "      <th>perceived_colour_master_id</th>\n",
       "      <th>department_no</th>\n",
       "      <th>index_code</th>\n",
       "      <th>index_group_no</th>\n",
       "      <th>section_no</th>\n",
       "      <th>garment_group_no</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0108775015</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0108775044</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0108775051</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010017</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0110065001</td>\n",
       "      <td>110065</td>\n",
       "      <td>306</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1339</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "      <td>1017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0110065002</td>\n",
       "      <td>110065</td>\n",
       "      <td>306</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>1339</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "      <td>1017</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   article_id  product_code  product_type_no  product_group_name  \\\n",
       "0  0108775015        108775              253  Garment Upper body   \n",
       "1  0108775044        108775              253  Garment Upper body   \n",
       "2  0108775051        108775              253  Garment Upper body   \n",
       "3  0110065001        110065              306           Underwear   \n",
       "4  0110065002        110065              306           Underwear   \n",
       "\n",
       "   graphical_appearance_no  colour_group_code  perceived_colour_value_id  \\\n",
       "0                  1010016                  9                          4   \n",
       "1                  1010016                 10                          3   \n",
       "2                  1010017                 11                          1   \n",
       "3                  1010016                  9                          4   \n",
       "4                  1010016                 10                          3   \n",
       "\n",
       "   perceived_colour_master_id  department_no index_code  index_group_no  \\\n",
       "0                           5           1676          A               1   \n",
       "1                           9           1676          A               1   \n",
       "2                           9           1676          A               1   \n",
       "3                           5           1339          B               1   \n",
       "4                           9           1339          B               1   \n",
       "\n",
       "   section_no  garment_group_no  \n",
       "0          16              1002  \n",
       "1          16              1002  \n",
       "2          16              1002  \n",
       "3          61              1017  \n",
       "4          61              1017  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.drop(columns = ['product_type_name', 'graphical_appearance_name', 'colour_group_name', 'perceived_colour_value_name',\n",
    "                        'perceived_colour_master_name', 'index_name', 'index_group_name', 'section_name', \n",
    "                        'garment_group_name', 'prod_name', 'department_name', 'detail_desc'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7f4269f2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:42:02.035677Z",
     "iopub.status.busy": "2022-03-30T00:42:02.034319Z",
     "iopub.status.idle": "2022-03-30T00:42:02.053128Z",
     "shell.execute_reply": "2022-03-30T00:42:02.053834Z",
     "shell.execute_reply.started": "2022-03-20T03:28:33.667553Z"
    },
    "papermill": {
     "duration": 0.101736,
     "end_time": "2022-03-30T00:42:02.054009",
     "exception": false,
     "start_time": "2022-03-30T00:42:01.952273",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>item_id:token</th>\n",
       "      <th>product_code:token</th>\n",
       "      <th>product_type_no:float</th>\n",
       "      <th>product_group_name:token_seq</th>\n",
       "      <th>graphical_appearance_no:token</th>\n",
       "      <th>colour_group_code:token</th>\n",
       "      <th>perceived_colour_value_id:token</th>\n",
       "      <th>perceived_colour_master_id:token</th>\n",
       "      <th>department_no:token</th>\n",
       "      <th>index_code:token</th>\n",
       "      <th>index_group_no:token</th>\n",
       "      <th>section_no:token</th>\n",
       "      <th>garment_group_no:token</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0108775015</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0108775044</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010016</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0108775051</td>\n",
       "      <td>108775</td>\n",
       "      <td>253</td>\n",
       "      <td>Garment Upper body</td>\n",
       "      <td>1010017</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>1676</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0110065001</td>\n",
       "      <td>110065</td>\n",
       "      <td>306</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1339</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "      <td>1017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0110065002</td>\n",
       "      <td>110065</td>\n",
       "      <td>306</td>\n",
       "      <td>Underwear</td>\n",
       "      <td>1010016</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>1339</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "      <td>1017</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  item_id:token  product_code:token  product_type_no:float  \\\n",
       "0    0108775015              108775                    253   \n",
       "1    0108775044              108775                    253   \n",
       "2    0108775051              108775                    253   \n",
       "3    0110065001              110065                    306   \n",
       "4    0110065002              110065                    306   \n",
       "\n",
       "  product_group_name:token_seq  graphical_appearance_no:token  \\\n",
       "0           Garment Upper body                        1010016   \n",
       "1           Garment Upper body                        1010016   \n",
       "2           Garment Upper body                        1010017   \n",
       "3                    Underwear                        1010016   \n",
       "4                    Underwear                        1010016   \n",
       "\n",
       "   colour_group_code:token  perceived_colour_value_id:token  \\\n",
       "0                        9                                4   \n",
       "1                       10                                3   \n",
       "2                       11                                1   \n",
       "3                        9                                4   \n",
       "4                       10                                3   \n",
       "\n",
       "   perceived_colour_master_id:token  department_no:token index_code:token  \\\n",
       "0                                 5                 1676                A   \n",
       "1                                 9                 1676                A   \n",
       "2                                 9                 1676                A   \n",
       "3                                 5                 1339                B   \n",
       "4                                 9                 1339                B   \n",
       "\n",
       "   index_group_no:token  section_no:token  garment_group_no:token  \n",
       "0                     1                16                    1002  \n",
       "1                     1                16                    1002  \n",
       "2                     1                16                    1002  \n",
       "3                     1                61                    1017  \n",
       "4                     1                61                    1017  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = df.rename(\n",
    "    columns={'article_id': 'item_id:token', 'product_code': 'product_code:token', 'product_type_no': 'product_type_no:float',\n",
    "             'product_group_name': 'product_group_name:token_seq', 'graphical_appearance_no': 'graphical_appearance_no:token', \n",
    "             'colour_group_code': 'colour_group_code:token', 'perceived_colour_value_id': 'perceived_colour_value_id:token', \n",
    "             'perceived_colour_master_id': 'perceived_colour_master_id:token', 'department_no': 'department_no:token', \n",
    "             'index_code': 'index_code:token', 'index_group_no': 'index_group_no:token', 'section_no': 'section_no:token', \n",
    "             'garment_group_no': 'garment_group_no:token'})\n",
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3caa3a90",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:42:02.208337Z",
     "iopub.status.busy": "2022-03-30T00:42:02.207395Z",
     "iopub.status.idle": "2022-03-30T00:42:03.458221Z",
     "shell.execute_reply": "2022-03-30T00:42:03.457583Z",
     "shell.execute_reply.started": "2022-03-20T03:28:33.691403Z"
    },
    "papermill": {
     "duration": 1.330234,
     "end_time": "2022-03-30T00:42:03.458383",
     "exception": false,
     "start_time": "2022-03-30T00:42:02.128149",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!mkdir /kaggle/working/recbox_data\n",
    "temp.to_csv(r'/kaggle/working/recbox_data/recbox_data.item', index=False, sep='\\t')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3466077e",
   "metadata": {
    "papermill": {
     "duration": 0.071684,
     "end_time": "2022-03-30T00:42:03.602471",
     "exception": false,
     "start_time": "2022-03-30T00:42:03.530787",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 1.B create atomic of iteration features\n",
    "we will create iteration features for GRU4REC model "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1d0b41f8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:42:03.755739Z",
     "iopub.status.busy": "2022-03-30T00:42:03.754738Z",
     "iopub.status.idle": "2022-03-30T00:43:11.067732Z",
     "shell.execute_reply": "2022-03-30T00:43:11.068281Z",
     "shell.execute_reply.started": "2022-03-20T03:28:34.839528Z"
    },
    "papermill": {
     "duration": 67.393475,
     "end_time": "2022-03-30T00:43:11.068452",
     "exception": false,
     "start_time": "2022-03-30T00:42:03.674977",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>t_dat</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>article_id</th>\n",
       "      <th>price</th>\n",
       "      <th>sales_channel_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0663713001</td>\n",
       "      <td>0.050831</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0541518023</td>\n",
       "      <td>0.030492</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0505221004</td>\n",
       "      <td>0.015237</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687003</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687004</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        t_dat                                        customer_id  article_id  \\\n",
       "0  2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...  0663713001   \n",
       "1  2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...  0541518023   \n",
       "2  2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0505221004   \n",
       "3  2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0685687003   \n",
       "4  2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0685687004   \n",
       "\n",
       "      price  sales_channel_id  \n",
       "0  0.050831                 2  \n",
       "1  0.030492                 2  \n",
       "2  0.015237                 2  \n",
       "3  0.016932                 2  \n",
       "4  0.016932                 2  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(r\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\", \n",
    "                 dtype={'article_id': 'str'})\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5530304c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:11.675651Z",
     "iopub.status.busy": "2022-03-30T00:43:11.674406Z",
     "iopub.status.idle": "2022-03-30T00:43:17.613262Z",
     "shell.execute_reply": "2022-03-30T00:43:17.612408Z",
     "shell.execute_reply.started": "2022-03-20T03:30:08.593969Z"
    },
    "papermill": {
     "duration": 6.473316,
     "end_time": "2022-03-30T00:43:17.613404",
     "exception": false,
     "start_time": "2022-03-30T00:43:11.140088",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>t_dat</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>article_id</th>\n",
       "      <th>price</th>\n",
       "      <th>sales_channel_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0663713001</td>\n",
       "      <td>0.050831</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0541518023</td>\n",
       "      <td>0.030492</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0505221004</td>\n",
       "      <td>0.015237</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687003</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687004</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788319</th>\n",
       "      <td>2020-09-22</td>\n",
       "      <td>fff2282977442e327b45d8c89afde25617d00124d0f999...</td>\n",
       "      <td>0929511001</td>\n",
       "      <td>0.059305</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788320</th>\n",
       "      <td>2020-09-22</td>\n",
       "      <td>fff2282977442e327b45d8c89afde25617d00124d0f999...</td>\n",
       "      <td>0891322004</td>\n",
       "      <td>0.042356</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788321</th>\n",
       "      <td>2020-09-22</td>\n",
       "      <td>fff380805474b287b05cb2a7507b9a013482f7dd0bce0e...</td>\n",
       "      <td>0918325001</td>\n",
       "      <td>0.043203</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788322</th>\n",
       "      <td>2020-09-22</td>\n",
       "      <td>fff4d3a8b1f3b60af93e78c30a7cb4cf75edaf2590d3e5...</td>\n",
       "      <td>0833459002</td>\n",
       "      <td>0.006763</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788323</th>\n",
       "      <td>2020-09-22</td>\n",
       "      <td>fffef3b6b73545df065b521e19f64bf6fe93bfd450ab20...</td>\n",
       "      <td>0898573003</td>\n",
       "      <td>0.033881</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>31788324 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              t_dat                                        customer_id  \\\n",
       "0        2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "1        2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "2        2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...   \n",
       "3        2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...   \n",
       "4        2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...   \n",
       "...             ...                                                ...   \n",
       "31788319 2020-09-22  fff2282977442e327b45d8c89afde25617d00124d0f999...   \n",
       "31788320 2020-09-22  fff2282977442e327b45d8c89afde25617d00124d0f999...   \n",
       "31788321 2020-09-22  fff380805474b287b05cb2a7507b9a013482f7dd0bce0e...   \n",
       "31788322 2020-09-22  fff4d3a8b1f3b60af93e78c30a7cb4cf75edaf2590d3e5...   \n",
       "31788323 2020-09-22  fffef3b6b73545df065b521e19f64bf6fe93bfd450ab20...   \n",
       "\n",
       "          article_id     price  sales_channel_id  \n",
       "0         0663713001  0.050831                 2  \n",
       "1         0541518023  0.030492                 2  \n",
       "2         0505221004  0.015237                 2  \n",
       "3         0685687003  0.016932                 2  \n",
       "4         0685687004  0.016932                 2  \n",
       "...              ...       ...               ...  \n",
       "31788319  0929511001  0.059305                 2  \n",
       "31788320  0891322004  0.042356                 2  \n",
       "31788321  0918325001  0.043203                 1  \n",
       "31788322  0833459002  0.006763                 1  \n",
       "31788323  0898573003  0.033881                 2  \n",
       "\n",
       "[31788324 rows x 5 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['t_dat'] = pd.to_datetime(df['t_dat'], format=\"%Y-%m-%d\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d0d23d57",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:17.875610Z",
     "iopub.status.busy": "2022-03-30T00:43:17.874533Z",
     "iopub.status.idle": "2022-03-30T00:43:18.680561Z",
     "shell.execute_reply": "2022-03-30T00:43:18.681754Z",
     "shell.execute_reply.started": "2022-03-20T03:30:14.348681Z"
    },
    "papermill": {
     "duration": 0.973379,
     "end_time": "2022-03-30T00:43:18.681994",
     "exception": false,
     "start_time": "2022-03-30T00:43:17.708615",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>t_dat</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>article_id</th>\n",
       "      <th>price</th>\n",
       "      <th>sales_channel_id</th>\n",
       "      <th>timestamp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0663713001</td>\n",
       "      <td>0.050831</td>\n",
       "      <td>2</td>\n",
       "      <td>1537401600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0541518023</td>\n",
       "      <td>0.030492</td>\n",
       "      <td>2</td>\n",
       "      <td>1537401600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0505221004</td>\n",
       "      <td>0.015237</td>\n",
       "      <td>2</td>\n",
       "      <td>1537401600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687003</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "      <td>1537401600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-09-20</td>\n",
       "      <td>00007d2de826758b65a93dd24ce629ed66842531df6699...</td>\n",
       "      <td>0685687004</td>\n",
       "      <td>0.016932</td>\n",
       "      <td>2</td>\n",
       "      <td>1537401600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       t_dat                                        customer_id  article_id  \\\n",
       "0 2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...  0663713001   \n",
       "1 2018-09-20  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...  0541518023   \n",
       "2 2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0505221004   \n",
       "3 2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0685687003   \n",
       "4 2018-09-20  00007d2de826758b65a93dd24ce629ed66842531df6699...  0685687004   \n",
       "\n",
       "      price  sales_channel_id   timestamp  \n",
       "0  0.050831                 2  1537401600  \n",
       "1  0.030492                 2  1537401600  \n",
       "2  0.015237                 2  1537401600  \n",
       "3  0.016932                 2  1537401600  \n",
       "4  0.016932                 2  1537401600  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "df['timestamp'] = df.t_dat.values.astype(np.int64) // 10 ** 9\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04aad5a7",
   "metadata": {
    "papermill": {
     "duration": 0.121777,
     "end_time": "2022-03-30T00:43:18.925901",
     "exception": false,
     "start_time": "2022-03-30T00:43:18.804124",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "**We fill with data in only 2020(timestamp > > 1585620000) and create inter file**\n",
    "For anyone need instruction about inter file, please check below links:\n",
    "* https://recbole.io/docs/user_guide/data_intro.html\n",
    "* https://recbole.io/docs/user_guide/data/atomic_files.html\n",
    "\n",
    "if you want a full of iterations without limiting timestamp, please check here:\n",
    "* "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "451144d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:19.168703Z",
     "iopub.status.busy": "2022-03-30T00:43:19.167608Z",
     "iopub.status.idle": "2022-03-30T00:43:20.860491Z",
     "shell.execute_reply": "2022-03-30T00:43:20.861014Z",
     "shell.execute_reply.started": "2022-03-20T03:30:14.866117Z"
    },
    "papermill": {
     "duration": 1.812346,
     "end_time": "2022-03-30T00:43:20.861202",
     "exception": false,
     "start_time": "2022-03-30T00:43:19.048856",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id:token</th>\n",
       "      <th>item_id:token</th>\n",
       "      <th>timestamp:float</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23934157</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0727808001</td>\n",
       "      <td>1585699200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23934158</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0727808007</td>\n",
       "      <td>1585699200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23934159</th>\n",
       "      <td>000563485cbb7850b0a93c6606f89c5b961c6647d1bd48...</td>\n",
       "      <td>0567532015</td>\n",
       "      <td>1585699200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23934160</th>\n",
       "      <td>000563485cbb7850b0a93c6606f89c5b961c6647d1bd48...</td>\n",
       "      <td>0706104009</td>\n",
       "      <td>1585699200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23934161</th>\n",
       "      <td>00083cda041544b2fbb0e0d2905ad17da7cf1007526fb4...</td>\n",
       "      <td>0783504004</td>\n",
       "      <td>1585699200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788319</th>\n",
       "      <td>fff2282977442e327b45d8c89afde25617d00124d0f999...</td>\n",
       "      <td>0929511001</td>\n",
       "      <td>1600732800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788320</th>\n",
       "      <td>fff2282977442e327b45d8c89afde25617d00124d0f999...</td>\n",
       "      <td>0891322004</td>\n",
       "      <td>1600732800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788321</th>\n",
       "      <td>fff380805474b287b05cb2a7507b9a013482f7dd0bce0e...</td>\n",
       "      <td>0918325001</td>\n",
       "      <td>1600732800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788322</th>\n",
       "      <td>fff4d3a8b1f3b60af93e78c30a7cb4cf75edaf2590d3e5...</td>\n",
       "      <td>0833459002</td>\n",
       "      <td>1600732800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31788323</th>\n",
       "      <td>fffef3b6b73545df065b521e19f64bf6fe93bfd450ab20...</td>\n",
       "      <td>0898573003</td>\n",
       "      <td>1600732800</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7854167 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              user_id:token item_id:token  \\\n",
       "23934157  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...    0727808001   \n",
       "23934158  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...    0727808007   \n",
       "23934159  000563485cbb7850b0a93c6606f89c5b961c6647d1bd48...    0567532015   \n",
       "23934160  000563485cbb7850b0a93c6606f89c5b961c6647d1bd48...    0706104009   \n",
       "23934161  00083cda041544b2fbb0e0d2905ad17da7cf1007526fb4...    0783504004   \n",
       "...                                                     ...           ...   \n",
       "31788319  fff2282977442e327b45d8c89afde25617d00124d0f999...    0929511001   \n",
       "31788320  fff2282977442e327b45d8c89afde25617d00124d0f999...    0891322004   \n",
       "31788321  fff380805474b287b05cb2a7507b9a013482f7dd0bce0e...    0918325001   \n",
       "31788322  fff4d3a8b1f3b60af93e78c30a7cb4cf75edaf2590d3e5...    0833459002   \n",
       "31788323  fffef3b6b73545df065b521e19f64bf6fe93bfd450ab20...    0898573003   \n",
       "\n",
       "          timestamp:float  \n",
       "23934157       1585699200  \n",
       "23934158       1585699200  \n",
       "23934159       1585699200  \n",
       "23934160       1585699200  \n",
       "23934161       1585699200  \n",
       "...                   ...  \n",
       "31788319       1600732800  \n",
       "31788320       1600732800  \n",
       "31788321       1600732800  \n",
       "31788322       1600732800  \n",
       "31788323       1600732800  \n",
       "\n",
       "[7854167 rows x 3 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = df[df['timestamp'] > 1585620000][['customer_id', 'article_id', 'timestamp']].rename(\n",
    "    columns={'customer_id': 'user_id:token', 'article_id': 'item_id:token', 'timestamp': 'timestamp:float'})\n",
    "temp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c8a6b12",
   "metadata": {
    "papermill": {
     "duration": 0.075369,
     "end_time": "2022-03-30T00:43:21.011379",
     "exception": false,
     "start_time": "2022-03-30T00:43:20.936010",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "We save atomic file in dataset format for using with recbole"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "08430886",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:21.175635Z",
     "iopub.status.busy": "2022-03-30T00:43:21.174627Z",
     "iopub.status.idle": "2022-03-30T00:43:57.249592Z",
     "shell.execute_reply": "2022-03-30T00:43:57.250218Z",
     "shell.execute_reply.started": "2022-03-20T03:30:16.32046Z"
    },
    "papermill": {
     "duration": 36.163301,
     "end_time": "2022-03-30T00:43:57.250409",
     "exception": false,
     "start_time": "2022-03-30T00:43:21.087108",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "160"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp.to_csv('/kaggle/working/recbox_data/recbox_data.inter', index=False, sep='\\t')\n",
    "del temp\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3fd7cf5",
   "metadata": {
    "papermill": {
     "duration": 0.076297,
     "end_time": "2022-03-30T00:43:57.402842",
     "exception": false,
     "start_time": "2022-03-30T00:43:57.326545",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 2. We create defautl recomendation for user who can not be predicted by sequential model.\n",
    "I use this approach in notebook: https://www.kaggle.com/hervind/h-m-faster-trending-products-weekly You can check it for more detail information. I will juse copy only code here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4950cf8b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:57.559376Z",
     "iopub.status.busy": "2022-03-30T00:43:57.558307Z",
     "iopub.status.idle": "2022-03-30T00:43:57.561403Z",
     "shell.execute_reply": "2022-03-30T00:43:57.560913Z",
     "shell.execute_reply.started": "2022-03-20T03:30:50.636671Z"
    },
    "papermill": {
     "duration": 0.083973,
     "end_time": "2022-03-30T00:43:57.561578",
     "exception": false,
     "start_time": "2022-03-30T00:43:57.477605",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "04e3fb11",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:43:57.720033Z",
     "iopub.status.busy": "2022-03-30T00:43:57.719431Z",
     "iopub.status.idle": "2022-03-30T00:44:50.466451Z",
     "shell.execute_reply": "2022-03-30T00:44:50.467107Z",
     "shell.execute_reply.started": "2022-03-20T03:30:50.642822Z"
    },
    "papermill": {
     "duration": 52.830362,
     "end_time": "2022-03-30T00:44:50.467278",
     "exception": false,
     "start_time": "2022-03-30T00:43:57.636916",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1371980, 2), (1371980, 2), (1371980, 2))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub0 = pd.read_csv('../input/hm-pre-recommendation/submissio_byfone_chris.csv').sort_values('customer_id').reset_index(drop=True)\n",
    "sub1 = pd.read_csv('../input/hm-pre-recommendation/submission_trending.csv').sort_values('customer_id').reset_index(drop=True)\n",
    "sub2 = pd.read_csv('../input/hm-pre-recommendation/submission_exponential_decay.csv').sort_values('customer_id').reset_index(drop=True)\n",
    "\n",
    "sub0.shape, sub1.shape, sub2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0c14ce6e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:44:50.626546Z",
     "iopub.status.busy": "2022-03-30T00:44:50.625317Z",
     "iopub.status.idle": "2022-03-30T00:44:50.887372Z",
     "shell.execute_reply": "2022-03-30T00:44:50.886795Z",
     "shell.execute_reply.started": "2022-03-20T03:31:44.176782Z"
    },
    "papermill": {
     "duration": 0.344738,
     "end_time": "2022-03-30T00:44:50.887513",
     "exception": false,
     "start_time": "2022-03-30T00:44:50.542775",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction0</th>\n",
       "      <th>prediction1</th>\n",
       "      <th>prediction2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 07...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 07...</td>\n",
       "      <td>0568601043 0924243001 0924243002 0918522001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0739590027 0723529001 08...</td>\n",
       "      <td>0826211002 0800436010 0739590027 0723529001 08...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 07...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 07...</td>\n",
       "      <td>0794321007 0924243001 0924243002 0918522001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0751471001 0706016001 06...</td>\n",
       "      <td>0448509014 0573085028 0751471001 0706016001 06...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0896152002 0818320001 09...</td>\n",
       "      <td>0730683050 0791587015 0896152002 0818320001 09...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                         prediction0  \\\n",
       "0  0568601043 0568601006 0656719005 0745232001 07...   \n",
       "1  0826211002 0800436010 0739590027 0723529001 08...   \n",
       "2  0794321007 0852643001 0852643003 0858883002 07...   \n",
       "3  0448509014 0573085028 0751471001 0706016001 06...   \n",
       "4  0730683050 0791587015 0896152002 0818320001 09...   \n",
       "\n",
       "                                         prediction1  \\\n",
       "0  0568601043 0568601006 0656719005 0745232001 07...   \n",
       "1  0826211002 0800436010 0739590027 0723529001 08...   \n",
       "2  0794321007 0852643001 0852643003 0858883002 07...   \n",
       "3  0448509014 0573085028 0751471001 0706016001 06...   \n",
       "4  0730683050 0791587015 0896152002 0818320001 09...   \n",
       "\n",
       "                                         prediction2  \n",
       "0  0568601043 0924243001 0924243002 0918522001 07...  \n",
       "1  0924243001 0924243002 0918522001 0751471001 04...  \n",
       "2  0794321007 0924243001 0924243002 0918522001 07...  \n",
       "3  0924243001 0924243002 0918522001 0751471001 04...  \n",
       "4  0924243001 0924243002 0918522001 0751471001 04...  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub0.columns = ['customer_id', 'prediction0']\n",
    "sub0['prediction1'] = sub1['prediction']\n",
    "sub0['prediction2'] = sub2['prediction']\n",
    "del sub1, sub2\n",
    "gc.collect()\n",
    "sub0.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5d85f39e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:44:51.043852Z",
     "iopub.status.busy": "2022-03-30T00:44:51.042783Z",
     "iopub.status.idle": "2022-03-30T00:48:22.448610Z",
     "shell.execute_reply": "2022-03-30T00:48:22.449190Z",
     "shell.execute_reply.started": "2022-03-20T03:31:44.407999Z"
    },
    "papermill": {
     "duration": 211.486514,
     "end_time": "2022-03-30T00:48:22.449364",
     "exception": false,
     "start_time": "2022-03-30T00:44:50.962850",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction0</th>\n",
       "      <th>prediction1</th>\n",
       "      <th>prediction2</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 07...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 07...</td>\n",
       "      <td>0568601043 0924243001 0924243002 0918522001 07...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0739590027 0723529001 08...</td>\n",
       "      <td>0826211002 0800436010 0739590027 0723529001 08...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 07...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 07...</td>\n",
       "      <td>0794321007 0924243001 0924243002 0918522001 07...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0751471001 0706016001 06...</td>\n",
       "      <td>0448509014 0573085028 0751471001 0706016001 06...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0896152002 0818320001 09...</td>\n",
       "      <td>0730683050 0791587015 0896152002 0818320001 09...</td>\n",
       "      <td>0924243001 0924243002 0918522001 0751471001 04...</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                         prediction0  \\\n",
       "0  0568601043 0568601006 0656719005 0745232001 07...   \n",
       "1  0826211002 0800436010 0739590027 0723529001 08...   \n",
       "2  0794321007 0852643001 0852643003 0858883002 07...   \n",
       "3  0448509014 0573085028 0751471001 0706016001 06...   \n",
       "4  0730683050 0791587015 0896152002 0818320001 09...   \n",
       "\n",
       "                                         prediction1  \\\n",
       "0  0568601043 0568601006 0656719005 0745232001 07...   \n",
       "1  0826211002 0800436010 0739590027 0723529001 08...   \n",
       "2  0794321007 0852643001 0852643003 0858883002 07...   \n",
       "3  0448509014 0573085028 0751471001 0706016001 06...   \n",
       "4  0730683050 0791587015 0896152002 0818320001 09...   \n",
       "\n",
       "                                         prediction2  \\\n",
       "0  0568601043 0924243001 0924243002 0918522001 07...   \n",
       "1  0924243001 0924243002 0918522001 0751471001 04...   \n",
       "2  0794321007 0924243001 0924243002 0918522001 07...   \n",
       "3  0924243001 0924243002 0918522001 0751471001 04...   \n",
       "4  0924243001 0924243002 0918522001 0751471001 04...   \n",
       "\n",
       "                                          prediction  \n",
       "0  0568601043 0568601006 0656719005 0745232001 09...  \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...  \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...  \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...  \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cust_blend(dt, W = [1,1,1]):\n",
    "    #Global ensemble weights\n",
    "    #W = [1.15,0.95,0.85]\n",
    "    \n",
    "    #Create a list of all model predictions\n",
    "    REC = []\n",
    "    REC.append(dt['prediction0'].split())\n",
    "    REC.append(dt['prediction1'].split())\n",
    "    REC.append(dt['prediction2'].split())\n",
    "    \n",
    "    #Create a dictionary of items recommended. \n",
    "    #Assign a weight according the order of appearance and multiply by global weights\n",
    "    res = {}\n",
    "    for M in range(len(REC)):\n",
    "        for n, v in enumerate(REC[M]):\n",
    "            if v in res:\n",
    "                res[v] += (W[M]/(n+1))\n",
    "            else:\n",
    "                res[v] = (W[M]/(n+1))\n",
    "    \n",
    "    # Sort dictionary by item weights\n",
    "    res = list(dict(sorted(res.items(), key=lambda item: -item[1])).keys())\n",
    "    \n",
    "    # Return the top 12 itens only\n",
    "    return ' '.join(res[:12])\n",
    "\n",
    "sub0['prediction'] = sub0.apply(cust_blend, W = [1.05,1.00,0.95], axis=1)\n",
    "sub0.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "26885807",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:48:22.718278Z",
     "iopub.status.busy": "2022-03-30T00:48:22.712658Z",
     "iopub.status.idle": "2022-03-30T00:48:33.560171Z",
     "shell.execute_reply": "2022-03-30T00:48:33.559554Z",
     "shell.execute_reply.started": "2022-03-20T03:34:31.621742Z"
    },
    "papermill": {
     "duration": 11.032183,
     "end_time": "2022-03-30T00:48:33.560355",
     "exception": false,
     "start_time": "2022-03-30T00:48:22.528172",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "del sub0['prediction0']\n",
    "del sub0['prediction1']\n",
    "del sub0['prediction2']\n",
    "gc.collect()\n",
    "sub0.to_csv(f'submission.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "31fc4d9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:48:33.809587Z",
     "iopub.status.busy": "2022-03-30T00:48:33.808573Z",
     "iopub.status.idle": "2022-03-30T00:48:33.812129Z",
     "shell.execute_reply": "2022-03-30T00:48:33.812627Z",
     "shell.execute_reply.started": "2022-03-20T03:34:42.724028Z"
    },
    "papermill": {
     "duration": 0.17302,
     "end_time": "2022-03-30T00:48:33.812807",
     "exception": false,
     "start_time": "2022-03-30T00:48:33.639787",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del sub0\n",
    "del df\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccca9df9",
   "metadata": {
    "papermill": {
     "duration": 0.08023,
     "end_time": "2022-03-30T00:48:33.974083",
     "exception": false,
     "start_time": "2022-03-30T00:48:33.893853",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 3. Create dataset and train model with Recbole\n",
    "\n",
    "For anyone need instruction document, please check this link: https://recbole.io/docs/user_guide/usage/use_modules.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f0ac4e7a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:48:34.139603Z",
     "iopub.status.busy": "2022-03-30T00:48:34.138634Z",
     "iopub.status.idle": "2022-03-30T00:48:37.030761Z",
     "shell.execute_reply": "2022-03-30T00:48:37.029689Z",
     "shell.execute_reply.started": "2022-03-20T03:34:42.811908Z"
    },
    "papermill": {
     "duration": 2.978139,
     "end_time": "2022-03-30T00:48:37.030966",
     "exception": false,
     "start_time": "2022-03-30T00:48:34.052827",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "from logging import getLogger\n",
    "from recbole.config import Config\n",
    "from recbole.data import create_dataset, data_preparation\n",
    "from recbole.model.sequential_recommender import GRU4RecF\n",
    "from recbole.trainer import Trainer\n",
    "from recbole.utils import init_seed, init_logger"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb2a6624",
   "metadata": {
    "papermill": {
     "duration": 0.080352,
     "end_time": "2022-03-30T00:48:37.191738",
     "exception": false,
     "start_time": "2022-03-30T00:48:37.111386",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "for limiting memory and time traning, we will filter for only using user who bought more than 40 items and item which is sold more than 40 times. If you want to train with more data, please change below config\n",
    "* user_inter_num_interval\n",
    "* item_inter_num_interval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ba356668",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:48:37.363904Z",
     "iopub.status.busy": "2022-03-30T00:48:37.362514Z",
     "iopub.status.idle": "2022-03-30T00:48:38.115093Z",
     "shell.execute_reply": "2022-03-30T00:48:37.856983Z",
     "shell.execute_reply.started": "2022-03-20T03:34:45.136018Z"
    },
    "papermill": {
     "duration": 0.842333,
     "end_time": "2022-03-30T00:48:38.115271",
     "exception": false,
     "start_time": "2022-03-30T00:48:37.272938",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "General Hyper Parameters:\n",
      "gpu_id = 0\n",
      "use_gpu = True\n",
      "seed = 2020\n",
      "state = INFO\n",
      "reproducibility = True\n",
      "data_path = /kaggle/working/recbox_data\n",
      "checkpoint_dir = saved\n",
      "show_progress = True\n",
      "save_dataset = False\n",
      "dataset_save_path = None\n",
      "save_dataloaders = False\n",
      "dataloaders_save_path = None\n",
      "log_wandb = False\n",
      "\n",
      "Training Hyper Parameters:\n",
      "epochs = 30\n",
      "train_batch_size = 2048\n",
      "learner = adam\n",
      "learning_rate = 0.001\n",
      "neg_sampling = None\n",
      "eval_step = 1\n",
      "stopping_step = 10\n",
      "clip_grad_norm = None\n",
      "weight_decay = 0.0\n",
      "loss_decimal_place = 4\n",
      "\n",
      "Evaluation Hyper Parameters:\n",
      "eval_args = {'split': {'RS': [10, 0, 0]}, 'group_by': 'user', 'order': 'TO', 'mode': 'full'}\n",
      "repeatable = True\n",
      "metrics = ['Recall', 'MRR', 'NDCG', 'Hit', 'Precision']\n",
      "topk = [10]\n",
      "valid_metric = MRR@10\n",
      "valid_metric_bigger = True\n",
      "eval_batch_size = 4096\n",
      "metric_decimal_place = 4\n",
      "\n",
      "Dataset Hyper Parameters:\n",
      "field_separator = \t\n",
      "seq_separator =  \n",
      "USER_ID_FIELD = user_id\n",
      "ITEM_ID_FIELD = item_id\n",
      "RATING_FIELD = rating\n",
      "TIME_FIELD = timestamp\n",
      "seq_len = None\n",
      "LABEL_FIELD = label\n",
      "threshold = None\n",
      "NEG_PREFIX = neg_\n",
      "load_col = {'inter': ['user_id', 'item_id', 'timestamp'], 'item': ['item_id', 'product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no', 'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id', 'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']}\n",
      "unload_col = None\n",
      "unused_col = None\n",
      "additional_feat_suffix = None\n",
      "rm_dup_inter = None\n",
      "val_interval = None\n",
      "filter_inter_by_user_or_item = True\n",
      "user_inter_num_interval = [40,inf)\n",
      "item_inter_num_interval = [40,inf)\n",
      "alias_of_user_id = None\n",
      "alias_of_item_id = None\n",
      "alias_of_entity_id = None\n",
      "alias_of_relation_id = None\n",
      "preload_weight = None\n",
      "normalize_field = None\n",
      "normalize_all = None\n",
      "ITEM_LIST_LENGTH_FIELD = item_length\n",
      "LIST_SUFFIX = _list\n",
      "MAX_ITEM_LIST_LENGTH = 50\n",
      "POSITION_FIELD = position_id\n",
      "HEAD_ENTITY_ID_FIELD = head_id\n",
      "TAIL_ENTITY_ID_FIELD = tail_id\n",
      "RELATION_ID_FIELD = relation_id\n",
      "ENTITY_ID_FIELD = entity_id\n",
      "benchmark_filename = None\n",
      "\n",
      "Other Hyper Parameters: \n",
      "wandb_project = recbole\n",
      "require_pow = False\n",
      "embedding_size = 64\n",
      "hidden_size = 128\n",
      "num_layers = 1\n",
      "dropout_prob = 0.3\n",
      "selected_features = ['product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no', 'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id', 'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']\n",
      "pooling_mode = sum\n",
      "loss_type = CE\n",
      "MODEL_TYPE = ModelType.SEQUENTIAL\n",
      "MODEL_INPUT_TYPE = InputType.POINTWISE\n",
      "eval_type = EvaluatorType.RANKING\n",
      "device = cuda\n",
      "train_neg_sample_args = {'strategy': 'none'}\n",
      "eval_neg_sample_args = {'strategy': 'full', 'distribution': 'uniform'}\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "parameter_dict = {\n",
    "    'data_path': '/kaggle/working',\n",
    "    'USER_ID_FIELD': 'user_id',\n",
    "    'ITEM_ID_FIELD': 'item_id',\n",
    "    'TIME_FIELD': 'timestamp',\n",
    "    'user_inter_num_interval': \"[40,inf)\",\n",
    "    'item_inter_num_interval': \"[40,inf)\",\n",
    "    'load_col': {'inter': ['user_id', 'item_id', 'timestamp'],\n",
    "                 'item': ['item_id', 'product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n",
    "                      'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n",
    "                      'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']\n",
    "             },\n",
    "    'selected_features': ['product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n",
    "                          'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n",
    "                          'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no'],\n",
    "    'neg_sampling': None,\n",
    "    'epochs': 30,\n",
    "    'eval_args': {\n",
    "        'split': {'RS': [10, 0, 0]},\n",
    "        'group_by': 'user',\n",
    "        'order': 'TO',\n",
    "        'mode': 'full'}\n",
    "}\n",
    "\n",
    "config = Config(model='GRU4RecF', dataset='recbox_data', config_dict=parameter_dict)\n",
    "\n",
    "# init random seed\n",
    "init_seed(config['seed'], config['reproducibility'])\n",
    "\n",
    "# logger initialization\n",
    "init_logger(config)\n",
    "logger = getLogger()\n",
    "# Create handlers\n",
    "c_handler = logging.StreamHandler()\n",
    "c_handler.setLevel(logging.INFO)\n",
    "logger.addHandler(c_handler)\n",
    "\n",
    "# write config info into log\n",
    "logger.info(config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "476c6883",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:48:38.561771Z",
     "iopub.status.busy": "2022-03-30T00:48:38.560009Z",
     "iopub.status.idle": "2022-03-30T00:50:10.595639Z",
     "shell.execute_reply": "2022-03-30T00:50:10.579753Z",
     "shell.execute_reply.started": "2022-03-20T03:34:45.454951Z"
    },
    "papermill": {
     "duration": 92.260334,
     "end_time": "2022-03-30T00:50:10.595802",
     "exception": false,
     "start_time": "2022-03-30T00:48:38.335468",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "recbox_data\n",
      "The number of users: 15459\n",
      "Average actions of users: 59.21956268598784\n",
      "The number of items: 7330\n",
      "Average actions of items: 124.9032610178742\n",
      "The number of inters: 915416\n",
      "The sparsity of the dataset: 99.19214553975321%\n",
      "Remain Fields: ['user_id', 'item_id', 'timestamp', 'product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no', 'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id', 'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']\n"
     ]
    }
   ],
   "source": [
    "dataset = create_dataset(config)\n",
    "logger.info(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e27072f2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:50:11.033273Z",
     "iopub.status.busy": "2022-03-30T00:50:11.032541Z",
     "iopub.status.idle": "2022-03-30T00:50:30.653716Z",
     "shell.execute_reply": "2022-03-30T00:50:30.648118Z",
     "shell.execute_reply.started": "2022-03-20T03:36:01.448124Z"
    },
    "papermill": {
     "duration": 19.845623,
     "end_time": "2022-03-30T00:50:30.653883",
     "exception": false,
     "start_time": "2022-03-30T00:50:10.808260",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Training]: train_batch_size = [2048] negative sampling: [None]\n",
      "[Evaluation]: eval_batch_size = [4096] eval_args: [{'split': {'RS': [10, 0, 0]}, 'group_by': 'user', 'order': 'TO', 'mode': 'full'}]\n"
     ]
    }
   ],
   "source": [
    "# dataset splitting\n",
    "train_data, valid_data, test_data = data_preparation(config, dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "89dbcdd3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T00:50:31.118124Z",
     "iopub.status.busy": "2022-03-30T00:50:31.117440Z",
     "iopub.status.idle": "2022-03-30T01:14:14.075339Z",
     "shell.execute_reply": "2022-03-30T01:14:14.074553Z",
     "shell.execute_reply.started": "2022-03-20T03:36:17.473947Z"
    },
    "papermill": {
     "duration": 1423.193275,
     "end_time": "2022-03-30T01:14:14.075541",
     "exception": false,
     "start_time": "2022-03-30T00:50:30.882266",
     "status": "completed"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "GRU4RecF(\n",
      "  (item_embedding): Embedding(7330, 64, padding_idx=0)\n",
      "  (feature_embed_layer): FeatureSeqEmbLayer(\n",
      "    (token_embedding_table): ModuleDict(\n",
      "      (item): FMEmbedding(\n",
      "        (embedding): Embedding(3935, 64)\n",
      "      )\n",
      "    )\n",
      "    (float_embedding_table): ModuleDict(\n",
      "      (item): Embedding(1, 64)\n",
      "    )\n",
      "    (token_seq_embedding_table): ModuleDict(\n",
      "      (item): ModuleList(\n",
      "        (0): Embedding(16, 64)\n",
      "      )\n",
      "    )\n",
      "  )\n",
      "  (item_gru_layers): GRU(64, 128, bias=False, batch_first=True)\n",
      "  (feature_gru_layers): GRU(768, 128, bias=False, batch_first=True)\n",
      "  (dense_layer): Linear(in_features=256, out_features=64, bias=True)\n",
      "  (dropout): Dropout(p=0.3, inplace=False)\n",
      "  (loss_fct): CrossEntropyLoss()\n",
      ")\n",
      "Trainable parameters: 1156288\n",
      "epoch 0 training [time: 47.65s, train loss: 3642.3637]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 1 training [time: 44.34s, train loss: 3390.6134]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 2 training [time: 44.25s, train loss: 3250.4472]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 3 training [time: 44.39s, train loss: 3163.3735]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 4 training [time: 44.24s, train loss: 3099.2533]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 5 training [time: 44.37s, train loss: 3044.1074]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 6 training [time: 44.19s, train loss: 2998.5542]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 7 training [time: 44.22s, train loss: 2962.4046]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 8 training [time: 44.27s, train loss: 2932.5592]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 9 training [time: 44.35s, train loss: 2907.5308]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 10 training [time: 44.34s, train loss: 2885.6282]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 11 training [time: 44.20s, train loss: 2867.5368]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 12 training [time: 44.19s, train loss: 2850.5957]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 13 training [time: 44.52s, train loss: 2836.0930]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 14 training [time: 44.21s, train loss: 2822.5238]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 15 training [time: 44.39s, train loss: 2811.0895]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 16 training [time: 44.43s, train loss: 2799.8698]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 17 training [time: 44.28s, train loss: 2790.4907]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 18 training [time: 44.26s, train loss: 2781.8785]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 19 training [time: 44.24s, train loss: 2774.2283]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 20 training [time: 44.68s, train loss: 2766.8078]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 21 training [time: 44.25s, train loss: 2760.0341]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 22 training [time: 44.42s, train loss: 2753.8305]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 23 training [time: 44.29s, train loss: 2748.0924]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 24 training [time: 44.33s, train loss: 2742.5462]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 25 training [time: 44.21s, train loss: 2737.9435]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 26 training [time: 44.25s, train loss: 2732.9869]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 27 training [time: 44.63s, train loss: 2728.7535]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 28 training [time: 44.28s, train loss: 2724.6929]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n",
      "epoch 29 training [time: 44.38s, train loss: 2720.6401]\n",
      "Saving current: saved/GRU4RecF-Mar-30-2022_00-50-40.pth\n"
     ]
    }
   ],
   "source": [
    "# model loading and initialization\n",
    "model = GRU4RecF(config, train_data.dataset).to(config['device'])\n",
    "logger.info(model)\n",
    "\n",
    "# trainer loading and initialization\n",
    "trainer = Trainer(config, model)\n",
    "\n",
    "# model training\n",
    "best_valid_score, best_valid_result = trainer.fit(train_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f40b6bc6",
   "metadata": {
    "papermill": {
     "duration": 0.347651,
     "end_time": "2022-03-30T01:14:14.780591",
     "exception": false,
     "start_time": "2022-03-30T01:14:14.432940",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 4. Create recommendation result from trained model\n",
    "\n",
    "I note document here for any one want to customize it: https://recbole.io/docs/user_guide/usage/case_study.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9cbfb308",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:14:15.484564Z",
     "iopub.status.busy": "2022-03-30T01:14:15.483621Z",
     "iopub.status.idle": "2022-03-30T01:14:15.492929Z",
     "shell.execute_reply": "2022-03-30T01:14:15.492376Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.568423Z"
    },
    "papermill": {
     "duration": 0.361155,
     "end_time": "2022-03-30T01:14:15.493104",
     "exception": false,
     "start_time": "2022-03-30T01:14:15.131949",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from recbole.utils.case_study import full_sort_topk\n",
    "external_user_ids = dataset.id2token(\n",
    "    dataset.uid_field, list(range(dataset.user_num)))[1:]#fist element in array is 'PAD'(default of Recbole) ->remove it "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d7e54de4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:14:16.564600Z",
     "iopub.status.busy": "2022-03-30T01:14:16.563632Z",
     "iopub.status.idle": "2022-03-30T01:14:16.567396Z",
     "shell.execute_reply": "2022-03-30T01:14:16.567950Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.570281Z"
    },
    "papermill": {
     "duration": 0.544809,
     "end_time": "2022-03-30T01:14:16.568142",
     "exception": false,
     "start_time": "2022-03-30T01:14:16.023333",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from recbole.data.interaction import Interaction\n",
    "\n",
    "def add_last_item(old_interaction, last_item_id, max_len=50):\n",
    "    new_seq_items = old_interaction['item_id_list'][-1]\n",
    "    if old_interaction['item_length'][-1].item() < max_len:\n",
    "        new_seq_items[old_interaction['item_length'][-1].item()] = last_item_id\n",
    "    else:\n",
    "        new_seq_items = torch.roll(new_seq_items, -1)\n",
    "        new_seq_items[-1] = last_item_id\n",
    "    return new_seq_items.view(1, len(new_seq_items))\n",
    "\n",
    "def predict_for_all_item(external_user_id, dataset, model):\n",
    "    model.eval()\n",
    "    with torch.no_grad():\n",
    "        uid_series = dataset.token2id(dataset.uid_field, [external_user_id])\n",
    "        index = np.isin(dataset.inter_feat[dataset.uid_field].numpy(), uid_series)\n",
    "        input_interaction = dataset[index]\n",
    "        test = {\n",
    "            'item_id_list': add_last_item(input_interaction, \n",
    "                                          input_interaction['item_id'][-1].item(), model.max_seq_length),\n",
    "            'item_length': torch.tensor(\n",
    "                [input_interaction['item_length'][-1].item() + 1\n",
    "                 if input_interaction['item_length'][-1].item() < model.max_seq_length else model.max_seq_length])\n",
    "        }\n",
    "        new_inter = Interaction(test)\n",
    "        new_inter = new_inter.to(config['device'])\n",
    "        new_scores = model.full_sort_predict(new_inter)\n",
    "        new_scores = new_scores.view(-1, test_data.dataset.item_num)\n",
    "        new_scores[:, 0] = -np.inf  # set scores of [pad] to -inf\n",
    "    return torch.topk(new_scores, 12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e6c29c5a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:14:17.240185Z",
     "iopub.status.busy": "2022-03-30T01:14:17.239246Z",
     "iopub.status.idle": "2022-03-30T01:14:17.432694Z",
     "shell.execute_reply": "2022-03-30T01:14:17.433385Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.572226Z"
    },
    "papermill": {
     "duration": 0.523213,
     "end_time": "2022-03-30T01:14:17.433563",
     "exception": false,
     "start_time": "2022-03-30T01:14:16.910350",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.return_types.topk(\n",
       "values=tensor([[8.2494, 7.3775, 7.2640, 6.9034, 6.8279, 6.5702, 6.4665, 6.2845, 6.0385,\n",
       "         6.0317, 5.9852, 5.9483]], device='cuda:0'),\n",
       "indices=tensor([[4559, 1608, 5835, 4529, 1187, 5412,  371, 2589, 4579,  638, 2019, 2415]],\n",
       "       device='cuda:0'))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_for_all_item('0109ad0b5a76924a1b58be677409bb601cc8bead9a87b8ce5b08a4a1f5bc71ef', \n",
    "                     dataset, model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "47107721",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:14:18.083485Z",
     "iopub.status.busy": "2022-03-30T01:14:18.082537Z",
     "iopub.status.idle": "2022-03-30T01:20:10.117399Z",
     "shell.execute_reply": "2022-03-30T01:20:10.116413Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.574183Z"
    },
    "papermill": {
     "duration": 352.362944,
     "end_time": "2022-03-30T01:20:10.117614",
     "exception": false,
     "start_time": "2022-03-30T01:14:17.754670",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15458\n"
     ]
    }
   ],
   "source": [
    "topk_items = []\n",
    "for external_user_id in external_user_ids:\n",
    "    _, topk_iid_list = predict_for_all_item(external_user_id, dataset, model)\n",
    "    last_topk_iid_list = topk_iid_list[-1]\n",
    "    external_item_list = dataset.id2token(dataset.iid_field, last_topk_iid_list.cpu()).tolist()\n",
    "    topk_items.append(external_item_list)\n",
    "print(len(topk_items))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4c446bd6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:10.838441Z",
     "iopub.status.busy": "2022-03-30T01:20:10.837277Z",
     "iopub.status.idle": "2022-03-30T01:20:10.854113Z",
     "shell.execute_reply": "2022-03-30T01:20:10.853536Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.575588Z"
    },
    "papermill": {
     "duration": 0.357176,
     "end_time": "2022-03-30T01:20:10.854291",
     "exception": false,
     "start_time": "2022-03-30T01:20:10.497115",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00d7ebd46f6a6d53630d41386b6ef6a505cdc4c80011ff...</td>\n",
       "      <td>0918522001 0910601003 0673677022 0910601002 08...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0109ad0b5a76924a1b58be677409bb601cc8bead9a87b8...</td>\n",
       "      <td>0901955001 0833530002 0913030001 0861477001 08...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>013f00f9e218549246a3aa82b3f3a0c22a693bc25fa735...</td>\n",
       "      <td>0839402002 0865172003 0839402001 0770336001 08...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>01bada2a453b09c70ea57bdda5a9ef0fb04062718d3a3d...</td>\n",
       "      <td>0914441004 0724906006 0868874006 0867966009 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>01dd96059a11759518f10969d0a528f03c8501dc4c628b...</td>\n",
       "      <td>0891663002 0850244002 0817353008 0891663001 08...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00d7ebd46f6a6d53630d41386b6ef6a505cdc4c80011ff...   \n",
       "1  0109ad0b5a76924a1b58be677409bb601cc8bead9a87b8...   \n",
       "2  013f00f9e218549246a3aa82b3f3a0c22a693bc25fa735...   \n",
       "3  01bada2a453b09c70ea57bdda5a9ef0fb04062718d3a3d...   \n",
       "4  01dd96059a11759518f10969d0a528f03c8501dc4c628b...   \n",
       "\n",
       "                                          prediction  \n",
       "0  0918522001 0910601003 0673677022 0910601002 08...  \n",
       "1  0901955001 0833530002 0913030001 0861477001 08...  \n",
       "2  0839402002 0865172003 0839402001 0770336001 08...  \n",
       "3  0914441004 0724906006 0868874006 0867966009 07...  \n",
       "4  0891663002 0850244002 0817353008 0891663001 08...  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "external_item_str = [' '.join(x) for x in topk_items]\n",
    "result = pd.DataFrame(external_user_ids, columns=['customer_id'])\n",
    "result['prediction'] = external_item_str\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "76fed207",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:11.742604Z",
     "iopub.status.busy": "2022-03-30T01:20:11.549113Z",
     "iopub.status.idle": "2022-03-30T01:20:11.745644Z",
     "shell.execute_reply": "2022-03-30T01:20:11.746258Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.577985Z"
    },
    "papermill": {
     "duration": 0.563057,
     "end_time": "2022-03-30T01:20:11.746435",
     "exception": false,
     "start_time": "2022-03-30T01:20:11.183378",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del external_item_str\n",
    "del topk_items\n",
    "del external_user_ids\n",
    "del train_data\n",
    "del valid_data\n",
    "del test_data\n",
    "del model\n",
    "del Trainer\n",
    "del logger\n",
    "del dataset\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9bdc27a",
   "metadata": {
    "papermill": {
     "duration": 0.32871,
     "end_time": "2022-03-30T01:20:12.399473",
     "exception": false,
     "start_time": "2022-03-30T01:20:12.070763",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 5. Combine result from most bought items and GRU model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ac55b63e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:13.063277Z",
     "iopub.status.busy": "2022-03-30T01:20:13.062460Z",
     "iopub.status.idle": "2022-03-30T01:20:16.823768Z",
     "shell.execute_reply": "2022-03-30T01:20:16.823141Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.579843Z"
    },
    "papermill": {
     "duration": 4.096581,
     "end_time": "2022-03-30T01:20:16.823920",
     "exception": false,
     "start_time": "2022-03-30T01:20:12.727339",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1371980, 2)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df = pd.read_csv('submission.csv')\n",
    "submit_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c88aad11",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:17.491328Z",
     "iopub.status.busy": "2022-03-30T01:20:17.489060Z",
     "iopub.status.idle": "2022-03-30T01:20:17.495042Z",
     "shell.execute_reply": "2022-03-30T01:20:17.495635Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.581657Z"
    },
    "papermill": {
     "duration": 0.346787,
     "end_time": "2022-03-30T01:20:17.495829",
     "exception": false,
     "start_time": "2022-03-30T01:20:17.149042",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                          prediction  \n",
       "0  0568601043 0568601006 0656719005 0745232001 09...  \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...  \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...  \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...  \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "88c4db15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:18.936665Z",
     "iopub.status.busy": "2022-03-30T01:20:18.935582Z",
     "iopub.status.idle": "2022-03-30T01:20:19.413534Z",
     "shell.execute_reply": "2022-03-30T01:20:19.414149Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.583483Z"
    },
    "papermill": {
     "duration": 1.582283,
     "end_time": "2022-03-30T01:20:19.414370",
     "exception": false,
     "start_time": "2022-03-30T01:20:17.832087",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction_x</th>\n",
       "      <th>prediction_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                        prediction_x prediction_y  \n",
       "0  0568601043 0568601006 0656719005 0745232001 09...          NaN  \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...          NaN  \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...          NaN  \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...          NaN  \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...          NaN  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df = pd.merge(submit_df, result, on='customer_id', how='outer')\n",
    "submit_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b9381c71",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:20.617905Z",
     "iopub.status.busy": "2022-03-30T01:20:20.616834Z",
     "iopub.status.idle": "2022-03-30T01:20:49.699158Z",
     "shell.execute_reply": "2022-03-30T01:20:49.698558Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.58531Z"
    },
    "papermill": {
     "duration": 29.957585,
     "end_time": "2022-03-30T01:20:49.699340",
     "exception": false,
     "start_time": "2022-03-30T01:20:19.741755",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction_x</th>\n",
       "      <th>prediction_y</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "      <td>-1</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "      <td>-1</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "      <td>-1</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "      <td>-1</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "      <td>-1</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                        prediction_x prediction_y  \\\n",
       "0  0568601043 0568601006 0656719005 0745232001 09...           -1   \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...           -1   \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...           -1   \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...           -1   \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...           -1   \n",
       "\n",
       "                                          prediction  \n",
       "0  0568601043 0568601006 0656719005 0745232001 09...  \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...  \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...  \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...  \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df = submit_df.fillna(-1)\n",
    "submit_df['prediction'] = submit_df.apply(\n",
    "    lambda x: x['prediction_y'] if x['prediction_y'] != -1 else x['prediction_x'], axis=1)\n",
    "submit_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0b29f88a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:50.360135Z",
     "iopub.status.busy": "2022-03-30T01:20:50.358717Z",
     "iopub.status.idle": "2022-03-30T01:20:50.708834Z",
     "shell.execute_reply": "2022-03-30T01:20:50.709427Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.587134Z"
    },
    "papermill": {
     "duration": 0.684384,
     "end_time": "2022-03-30T01:20:50.709672",
     "exception": false,
     "start_time": "2022-03-30T01:20:50.025288",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...</td>\n",
       "      <td>0568601043 0568601006 0656719005 0745232001 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...</td>\n",
       "      <td>0826211002 0800436010 0924243001 0739590027 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...</td>\n",
       "      <td>0794321007 0852643001 0852643003 0858883002 09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...</td>\n",
       "      <td>0448509014 0573085028 0924243001 0751471001 07...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...</td>\n",
       "      <td>0730683050 0791587015 0924243001 0896152002 08...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         customer_id  \\\n",
       "0  00000dbacae5abe5e23885899a1fa44253a17956c6d1c3...   \n",
       "1  0000423b00ade91418cceaf3b26c6af3dd342b51fd051e...   \n",
       "2  000058a12d5b43e67d225668fa1f8d618c13dc232df0ca...   \n",
       "3  00005ca1c9ed5f5146b52ac8639a40ca9d57aeff4d1bd2...   \n",
       "4  00006413d8573cd20ed7128e53b7b13819fe5cfc2d801f...   \n",
       "\n",
       "                                          prediction  \n",
       "0  0568601043 0568601006 0656719005 0745232001 09...  \n",
       "1  0826211002 0800436010 0924243001 0739590027 07...  \n",
       "2  0794321007 0852643001 0852643003 0858883002 09...  \n",
       "3  0448509014 0573085028 0924243001 0751471001 07...  \n",
       "4  0730683050 0791587015 0924243001 0896152002 08...  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df = submit_df.drop(columns=['prediction_y', 'prediction_x'])\n",
    "submit_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0809a3da",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-30T01:20:51.371957Z",
     "iopub.status.busy": "2022-03-30T01:20:51.370974Z",
     "iopub.status.idle": "2022-03-30T01:21:02.714475Z",
     "shell.execute_reply": "2022-03-30T01:21:02.716457Z",
     "shell.execute_reply.started": "2022-03-20T04:00:34.588995Z"
    },
    "papermill": {
     "duration": 11.685594,
     "end_time": "2022-03-30T01:21:02.716804",
     "exception": false,
     "start_time": "2022-03-30T01:20:51.031210",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "submit_df.to_csv('submission.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96f1ddb7",
   "metadata": {
    "papermill": {
     "duration": 0.348025,
     "end_time": "2022-03-30T01:21:03.656677",
     "exception": false,
     "start_time": "2022-03-30T01:21:03.308652",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 2378.932098,
   "end_time": "2022-03-30T01:21:06.903964",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2022-03-30T00:41:27.971866",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
